package com.xbongbong.paas.util;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleExplainPojo;
import com.xbongbong.pro.businessrule.pojo.dto.OperationDTO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 吴峰
 * @date 2020/10/14 15:23
 */
public class BusinessRuleExplainUtil {


    /**
     * 获取子表单字段解释
     * @param operationDTO
     * @return com.xbongbong.pro.businessrule.pojo.BusinessRuleExplainPojo
     * @author 吴峰
     * @date 2020/10/10 14:54
     * @throws XbbException
     */
    public static BusinessRuleExplainPojo getSubExplainMap(OperationDTO operationDTO) {
        // 先将子表单的字段拿出来，因为子表单仅限于同一子表单，所以存下下来
        List<FilterConditionsPoJo> copyConditionList = new ArrayList<>();
        Map<String, FieldAttrEntity> targetExplainMap = operationDTO.getTargetExplain();
        Map<String, FieldAttrEntity> thisExplainMap = operationDTO.getThisExplain();
        List<FilterConditionsPoJo> filterConditionList = operationDTO.getFilterConditionList();
        Map<String, FieldAttrEntity> thisSubExplainMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<String, FieldAttrEntity> targetSubExplainMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        // 条件为：目标表单子表单内字段等于当前表单子表单内字段
        boolean isSubFormAndSubForm = false;
        // 条件为：目标表单子表单内字段等于当前表单主字段
        boolean isSubFormAndMain = false;
        // 条件为：目标表单为主字段等于当前表单为子表单字段
        boolean isMainAndSubForm = false;
        String thisKey = "";
        String targetKey = "";
        for (FilterConditionsPoJo item : filterConditionList) {
            if (item.getAttr().contains(FieldTypeEnum.SUB_FORM.getEsalias()) && item.getValueAttr().contains(FieldTypeEnum.SUB_FORM.getEsalias())) {
                copyConditionList.add(item);
                // 子赋值子
                isSubFormAndSubForm = true;
                if (thisSubExplainMap.isEmpty()) {
                    List<String> thisAttrList = ExplainUtil.getAttrByPoint(item.getValueAttr());

                    if (thisAttrList.size() == 2) {
                        thisKey = thisAttrList.get(0);
                    }
                    FieldAttrEntity fieldAttrEntity = thisExplainMap.get(thisKey);
                    List<? extends FieldAttrEntity> jsonArray = fieldAttrEntity.getSubForm().getItems();
                    thisSubExplainMap = ExplainUtil.getExplainMapByList(jsonArray);
                }
                if (targetSubExplainMap.isEmpty()) {
                    List<String> targetAttrList = ExplainUtil.getAttrByPoint(item.getAttr());
                    if (targetAttrList.size() == 2) {
                        targetKey = targetAttrList.get(0);
                        FieldAttrEntity fieldAttrEntity = targetExplainMap.get(targetKey);
                        List<? extends FieldAttrEntity> jsonArray = fieldAttrEntity.getSubForm().getItems();
                        targetSubExplainMap = ExplainUtil.getExplainMapByList(jsonArray);
                    }
                }
            }else if (item.getAttr().contains(FieldTypeEnum.SUB_FORM.getEsalias()) && !item.getValueAttr().contains(FieldTypeEnum.SUB_FORM.getEsalias())) {
                copyConditionList.add(item);
                // 当前表单字段赋值给目标表单子表单字段
                isSubFormAndMain = true;
                if (targetSubExplainMap.isEmpty()) {
                    List<String> targetAttrList = ExplainUtil.getAttrByPoint(item.getAttr());
                    if (targetAttrList.size() == 2) {
                        targetKey = targetAttrList.get(0);
                        FieldAttrEntity fieldAttrEntity = targetExplainMap.get(targetKey);
                        List<? extends FieldAttrEntity> jsonArray = fieldAttrEntity.getSubForm().getItems();
                        targetSubExplainMap = ExplainUtil.getExplainMapByList(jsonArray);
                    }
                }
            }else if (item.getValueAttr().contains(FieldTypeEnum.SUB_FORM.getEsalias()) && !item.getAttr().contains(FieldTypeEnum.SUB_FORM.getEsalias())) {
                copyConditionList.add(item);
                isMainAndSubForm = true;
                if (thisSubExplainMap.isEmpty()) {
                    List<String> thisAttrList = ExplainUtil.getAttrByPoint(item.getValueAttr());
                    if (thisAttrList.size() == 2) {
                        thisKey = thisAttrList.get(0);
                    }
                    FieldAttrEntity fieldAttrEntity = thisExplainMap.get(thisKey);
                    List<? extends FieldAttrEntity> jsonArray = fieldAttrEntity.getSubForm().getItems();
                    thisSubExplainMap = ExplainUtil.getExplainMapByList(jsonArray);
                }
            }
        }
        return new BusinessRuleExplainPojo(thisSubExplainMap, targetSubExplainMap, isSubFormAndSubForm, isSubFormAndMain, isMainAndSubForm, thisKey, targetKey, copyConditionList);
    }
}
